package com.rcn.web;

import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.rcn.core.Patient;
import com.rcn.core.exception.RaccoonException;
import com.rcn.sne.SneManager;

public class ManagerPatient 
{
	private static SneManager sneManager = new SneManager();
	private static Logger log = LogManager.getLogger(ManagerPatient.class.getName());
	
	/**
	 * Returns a list with all the patients
	 * @return
	 * @throws RaccoonException
	 */
	public List<Patient> getAllPatients() throws RaccoonException
	{
		log.debug("Getting all patients...");
		List<Patient> patientsList = sneManager.getAllPatients();
		log.debug(patientsList.size()+" patients founded.");
		return patientsList;
	}
	
	/**
	 * Delete a patient
	 * @param id Id of the patient who will be deleted.
	 */
	public void deletePatient(String id)
	{
		int patientId = Integer.parseInt(id);
		log.debug("Deleting patient. Id: "+patientId);
		try 
		{
			sneManager.deletePatient(patientId);
			log.debug("Patient deleted");
		} catch (RaccoonException e) {
			log.error(e.getMessage());
		}
	}
	
	/**
	 * Save a patient
	 * @param p
	 * @return
	 */
	public long savePatient(Patient p)
	{
		log.debug("Saving patient: "+p.getName()+" "+p.getSurname());
		long generatedId = -1;
		try {
			generatedId = sneManager.savePatient(p);
			log.debug("Patient saved");
		} catch (RaccoonException e) {
			log.error("An error ocurred trying to save a patient. "+ e.getMessage());
		}
		return generatedId;
	}
	
	public void updatePatient(Patient p)
	{
		log.debug("Updating patient: "+p.getName()+" "+p.getSurname());
		
		try {
			sneManager.updatePatient(p);
			log.debug("Patient updated");
		} catch (RaccoonException e) {
			log.error("An error ocurred trying to update a patient. "+ e.getMessage());
		}
	}
	/**
	 * Get a patient.
	 * @param id
	 * @return
	 */
	public Patient getPatient(long id)
	{
		log.debug("Gettin patient: "+id);
		Patient p = null;
		try {
			p = sneManager.getPatient(id);
			log.debug("Patient found");
		} catch (RaccoonException e) {
			log.error("An error ocurred trying to get a patient. "+e.getMessage());
		}
		return p;
	}
}
